﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SqlDataAccess;

namespace QuanLyBanHang
{
    public partial class F300_Danh_muc_nha_cung_cap : Form
    {
        public F300_Danh_muc_nha_cung_cap()
        {
            InitializeComponent();
            set_define_events();
        }

        #region Members
        BKIQuanLyBanHangEntities db = new BKIQuanLyBanHangEntities();
        #endregion

        #region Private Methods
        private bool check_validate_is_ok()
        {
            if (string.IsNullOrWhiteSpace(m_txt_ma_ncc.Text))
            {
                MessageBox.Show("Bạn phải nhập MÃ NHÀ CUNG CẤP!", "Thông báo!");
                m_txt_ma_ncc.Focus();
                return false;
            }
            if (string.IsNullOrWhiteSpace(m_txt_ten_ncc.Text))
            {
                MessageBox.Show("Bạn phải nhập TÊN NHÀ CUNG CẤP!", "Thông báo!");
                m_txt_ten_ncc.Focus();
                return false;
            }
            if (string.IsNullOrWhiteSpace(m_txt_dia_chi_ncc.Text))
            {
                MessageBox.Show("Bạn phải nhập ĐỊA CHỈ NHÀ CUNG CẤP!", "Thông báo!");
                m_txt_dia_chi_ncc.Focus();
                return false;
            }
            return true;
        }

        private void set_define_events()
        {
            this.Load +=F300_Danh_muc_nha_cung_cap_Load;
            this.m_cmd_cap_nhat_tt_ncc.Click += m_cmd_cap_nhat_tt_ncc_Click;
            this.m_cmd_quay_lai.Click += m_cmd_quay_lai_Click;
            this.m_cmd_tim_kiem_ncc.Click += m_cmd_tim_kiem_ncc_Click;
            this.m_cmd_them_ncc.Click += m_cmd_them_ncc_Click;
            this.m_cmd_xoa_ncc.Click += m_cmd_xoa_ncc_Click;
            this.m_cmd_xoa_trang.Click += m_cmd_xoa_trang_Click;
            this.m_txt_tim_kiem_ncc.TextChanged += m_txt_tim_kiem_ncc_TextChanged;
            this.m_grv_ncc.CellClick += m_grv_ncc_Click;

            //this.m_cmd_quay_lai.Click += new System.EventHandler(this.m_cmd_quay_lai_Click);
            //this.m_cmd_tim_kiem_ncc.Click += new System.EventHandler(this.m_cmd_tim_kiem_ncc_Click);
            //this.m_cmd_xoa_ncc.Click += new System.EventHandler(this.m_cmd_xoa_ncc_Click);
            //this.m_cmd_xoa_trang.Click += new System.EventHandler(this.m_cmd_xoa_trang_Click);
            //this.m_cmd_cap_nhat_tt_ncc.Click += new System.EventHandler(this.m_cmd_cap_nhat_tt_ncc_Click);
            //this.m_cmd_them_ncc.Click += new System.EventHandler(this.m_cmd_them_ncc_Click);
        }

        private void load_data_to_grid()
        {
            var v_lst_ncc = db.DM_NHA_CUNG_CAP
                .Where(x => x.MA_NCC.Contains(m_txt_tim_kiem_ncc.Text)
                || x.TEN_NCC.Contains(m_txt_tim_kiem_ncc.Text)
                || m_txt_tim_kiem_ncc.Text.Equals(""))
                .OrderBy(x => x.MA_NCC)
                .Select(x => new
                {
                    ID = x.ID,
                    MA_NCC = x.MA_NCC,
                    TEN_NCC = x.TEN_NCC,
                    DIA_CHI = x.DIA_CHI,
                    DIEN_THOAI = x.DIEN_THOAI,
                    FAX = x.FAX,
                    EMAIL = x.EMAIL
                })
                .ToList();
            m_grv_ncc.DataSource = v_lst_ncc;

            m_grv_ncc.Columns[0].HeaderText = "ID";
            m_grv_ncc.Columns[0].Visible = false;
            m_grv_ncc.Columns[1].HeaderText = "Mã NCC";
            m_grv_ncc.Columns[1].Width = 60;
            m_grv_ncc.Columns[2].HeaderText = "Tên NCC";
            m_grv_ncc.Columns[2].Width = 200;
            m_grv_ncc.Columns[3].HeaderText = "Địa chỉ";
            m_grv_ncc.Columns[3].Width = 200;
            m_grv_ncc.Columns[4].HeaderText = "SĐT";
            m_grv_ncc.Columns[4].Width = 70;
            m_grv_ncc.Columns[5].HeaderText = "Fax";
            m_grv_ncc.Columns[5].Visible = false;
            m_grv_ncc.Columns[6].HeaderText = "Email";
            m_grv_ncc.Columns[6].Width = 100;
            m_grv_ncc.Columns[6].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
        }
        #endregion

        #region Events
        private void m_cmd_them_ncc_Click(object sender, EventArgs e)
        {
            try
            {
                if (check_validate_is_ok())
                {
                    DM_NHA_CUNG_CAP v_dm_ncc = new DM_NHA_CUNG_CAP();
                    v_dm_ncc.MA_NCC = m_txt_ma_ncc.Text.Trim();
                    v_dm_ncc.TEN_NCC = m_txt_ten_ncc.Text.Trim();
                    v_dm_ncc.DIA_CHI = m_txt_dia_chi_ncc.Text.Trim();
                    v_dm_ncc.DIEN_THOAI = Convert.ToDecimal(m_txt_dien_thoai_ncc.Text.Trim());
                    v_dm_ncc.EMAIL = m_txt_email.Text.Trim();
                    v_dm_ncc.FAX = Convert.ToDecimal(m_txt_fax.Text.Trim());
                    db.DM_NHA_CUNG_CAP.Add(v_dm_ncc);
                    db.SaveChanges();
                    m_cmd_xoa_trang_Click(null, null);
                    MessageBox.Show("Bạn đã thêm thành công Nhà cung cấp!", "Thông báo!");
                    load_data_to_grid();
                }
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        private void m_cmd_cap_nhat_tt_ncc_Click(object sender, EventArgs e)
        {
            try
            {
                if (m_grv_ncc.CurrentRow == null) return;
                decimal id_ncc = (decimal)m_grv_ncc.CurrentRow.Cells[0].Value;
                DM_NHA_CUNG_CAP v_dm_ncc = db.DM_NHA_CUNG_CAP.FirstOrDefault(x => x.ID == id_ncc);
                if (v_dm_ncc != null)
                {
                    v_dm_ncc.MA_NCC = m_txt_ma_ncc.Text.Trim();
                    v_dm_ncc.TEN_NCC = m_txt_ten_ncc.Text.Trim();
                    v_dm_ncc.DIA_CHI = m_txt_dia_chi_ncc.Text.Trim();
                    v_dm_ncc.DIEN_THOAI = Convert.ToDecimal(m_txt_dien_thoai_ncc.Text.Trim());
                    v_dm_ncc.FAX = Convert.ToDecimal(m_txt_fax.Text.Trim());
                    v_dm_ncc.EMAIL = m_txt_email.Text.Trim();
                    db.SaveChanges();
                    m_cmd_xoa_trang_Click(null, null);
                    MessageBox.Show("Bạn đã cập nhật thành công nhà cung cấp!", "Thông báo!");
                    load_data_to_grid();
                }
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        private void m_cmd_xoa_trang_Click(object sender, EventArgs e)
        {
            try
            {
                m_txt_ma_ncc.Text = "";
                m_txt_ten_ncc.Text = "";
                m_txt_dia_chi_ncc.Text = "";
                m_txt_dien_thoai_ncc.Text = "";
                m_txt_email.Text = "";
                m_txt_fax.Text = "";
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        private void F300_Danh_muc_nha_cung_cap_Load(object sender, EventArgs e)
        {
            try
            {
                load_data_to_grid();
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        private void m_cmd_tim_kiem_ncc_Click(object sender, EventArgs e)
        {
            try
            {
                load_data_to_grid();
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        private void m_cmd_xoa_ncc_Click(object sender, EventArgs e)
        {
            try
            {
                if (m_grv_ncc.CurrentRow == null)
                {
                    MessageBox.Show("Bạn phải chọn 1 nhà cung cấp để xóa!", "Thông báo!");
                    return;
                }
                decimal id_ncc = (decimal)m_grv_ncc.CurrentRow.Cells[0].Value;
                DM_NHA_CUNG_CAP v_dm_ncc = db.DM_NHA_CUNG_CAP.FirstOrDefault(x => x.ID == id_ncc);
                if (v_dm_ncc != null)
                {
                    if (MessageBox.Show(@"Bạn có chắc chắn muốn xoá NHÀ CUNG CẤP này và DỮ LIỆU liên quan đến NCC này không?", "Cảnh báo!", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        //Xoa du lieu bang GD_HOA_DON_NHAP
                        var v_lst_hoa_don_nhap = db.GD_HOA_DON_NHAP
                                                    .Where(x => x.ID_NCC == id_ncc)
                                                    .ToList();
                        for (int i = 0; i < v_lst_hoa_don_nhap.Count; i++)
                        {
                            db.GD_HOA_DON_NHAP.Remove(v_lst_hoa_don_nhap[i]);
                            db.SaveChanges();
                        }
                        //Xoa du lieu bang DM_SAN_PHAM
                        var v_lst_dm_san_pham = db.DM_SAN_PHAM
                                                .Where(x => x.ID_NCC == id_ncc)
                                                .ToList();
                        for (int i = 0; i < v_lst_dm_san_pham.Count; i++)
                        {
                            db.DM_SAN_PHAM.Remove(v_lst_dm_san_pham[i]);
                            db.SaveChanges();
                        }
                        //Xoa du lieu bang DM_NHA_CUNG_CAP
                        db.DM_NHA_CUNG_CAP.Remove(v_dm_ncc);
                        db.SaveChanges();
                        MessageBox.Show("Bạn đã xóa thành công nhà cung cấp!", "Thông báo!");
                        load_data_to_grid();
                    }
                }
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        private void m_cmd_quay_lai_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        private void m_txt_tim_kiem_ncc_TextChanged(object sender, EventArgs e)
        {
            try
            {
                load_data_to_grid();
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        private void m_grv_ncc_Click(object sender, EventArgs e)
        {
            try
            {
                decimal id_ncc = (decimal)m_grv_ncc.CurrentRow.Cells[0].Value;
                DM_NHA_CUNG_CAP v_dm_ncc = db.DM_NHA_CUNG_CAP.FirstOrDefault(x => x.ID == id_ncc);
                if (v_dm_ncc != null)
                {
                    m_txt_ma_ncc.Text = v_dm_ncc.MA_NCC;
                    m_txt_ten_ncc.Text = v_dm_ncc.TEN_NCC;
                    m_txt_dia_chi_ncc.Text = v_dm_ncc.DIA_CHI;
                    m_txt_dien_thoai_ncc.Text = v_dm_ncc.DIEN_THOAI.ToString();
                    m_txt_email.Text = v_dm_ncc.EMAIL;
                    m_txt_fax.Text = v_dm_ncc.FAX.ToString();
                }
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }   
        }

        #endregion
    }
}
